﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace MethodWorx.Core.DomainModel
{
    /// <summary>
    /// Base class for any entities.  An entity in the domain model is something that can be "identified"
    /// by its ID.
    /// 
    /// RULES:
    ///    http://richarddingwall.name/2009/10/13/life-inside-an-aggregate-root-part-1/
    ///    
    ///    1. Entities can only hold references to aggregate roots, not entities or value objects within
    ///    2. Access to any entity or value object is only allowed via the root
    ///    3. The entire aggregate is locked, versioned and persisted together
    ///    
    /// </summary>
    /// 
    [Serializable]
    public abstract class Entity
    {
        public Entity()
        {
            this.Id = Guid.NewGuid();
        }

        /// <summary>
        /// The identity of the entity
        /// </summary>
        public Guid Id { get; set; }



        public override bool Equals(System.Object obj)
        {
            // If parameter is null return false.
            if (obj == null)
            {
                return false;
            }

            // If parameter cannot be cast to Point return false.
            Entity p = obj as Entity;
            if ((System.Object)p == null)
            {
                return false;
            }

            // Return true if the fields match:
            return (Id == p.Id);
        }

        public bool Equals(Entity p)
        {
            // If parameter is null return false:
            if ((object)p == null)
            {
                return false;
            }

            // Return true if the fields match:
            return (Id == p.Id);
        }

        public override string ToString()
        {
            return this.Id.ToString();
        }
    }
}
